import redis
import requests

AppId = 'wx74e5fb9d4d474f08'

AppSecret = 'ef9709fb8183d5f1ee60f958ae69ff11'

AccessUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(AppId,
                                                                                                             AppSecret)

REDIS_POOL = redis.ConnectionPool(host='114.116.123.190')  # 实现一个连接池


def get_access():


    sr = redis.Redis(connection_pool=REDIS_POOL)
    if sr.get('ACCESS_TOKEN'):
        # print("redis")
        ACCESS_TOKEN = sr.get('ACCESS_TOKEN').decode('utf-8')
    else:
        rsp = requests.get(AccessUrl).json()
        ACCESS_TOKEN = rsp.get('access_token')
        # print('重新')
        sr.setex('ACCESS_TOKEN', 7150, ACCESS_TOKEN)
    # print(rsp)
    return ACCESS_TOKEN

# get_access()